package cn.linran.org.controller;

import cn.linran.basic.annotation.PreAuthorize;
import cn.linran.basic.util.JsonResult;
import cn.linran.basic.util.PageList;
import cn.linran.org.domain.Department;
import cn.linran.org.query.DepartmentQuery;
import cn.linran.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // controller层，返回json格式数据
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "根据id部门",sn = "department:get")
    public Department findOne( @ApiParam(name="编号",required = true) @PathVariable("id") Long id){
        return departmentService.loadById(id);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除部门",sn = "department:delete")
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @PutMapping
    public JsonResult updateOrAdd(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.save(department);
            }else
                departmentService.update(department);
            return  JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        try {
            return departmentService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Department>();//第一次查
        }
    }
    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){

        try {
            departmentService.batchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }
    /**
     * 查询部门树
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    @PreAuthorize(name = "部门树",sn = "department:tree") //value后面用来做权限校验
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

    /**
     * http://localhost:8080/department  url
     * 动词： get
     * @return
     */
    // @RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping // 上面的结合
    @ApiOperation(value = "查询所有部门信息" )
    @PreAuthorize(name = "所有部门",sn = "department:getAll")
    public List<Department> findAll(){

        return departmentService.loadAll();
    }
}























